package com.focusai.efairy.ui.activity.warn;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;

import com.focusai.efairy.R;
import com.focusai.efairy.model.dev.DeviceItem;
import com.focusai.efairy.model.request.GetAlarmNodoListRequest;
import com.focusai.efairy.model.request.GetAnnounceListRequest;
import com.focusai.efairy.model.response.AlarmNodoResponse;
import com.focusai.efairy.network.HttpManager;
import com.focusai.efairy.network.Response;
import com.focusai.efairy.network.exception.NetworkException;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerAdapter;
import com.focusai.efairy.ui.adapter.base.BaseRecyclerViewHolder;
import com.focusai.efairy.ui.adapter.base.RecycleViewDivider;
import com.focusai.efairy.ui.adapter.warn.AlarmNodoAdapter;
import com.focusai.efairy.ui.base.SwipeBackTipBaseActivity;
import com.focusai.efairy.utils.ActivityUtils;
import com.focusai.efairy.utils.ScreenUtil;
import com.mylhyl.crlayout.SwipeRefreshAdapterView;
import com.mylhyl.crlayout.SwipeRefreshRecyclerView;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by luo_guangwen on 2017/10/18.
 */

public class AlarmNoDoListActivity extends SwipeBackTipBaseActivity implements SwipeRefreshLayout.OnRefreshListener,
        SwipeRefreshAdapterView.OnListLoadListener {
    private static final int REQUEST_CODE_ADD = 0x11;
    private static final int REQUEST_CODE_ITEM = 0x12;
    private SwipeRefreshRecyclerView mRefreshRecyclerView;
    private AlarmNodoAdapter mAdapter;
    private List<AlarmNodoResponse.AlarmDev> mList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_list);
        initView();
        setDefaultValues();
    }


    @Override
    protected void initView() {
        mRefreshRecyclerView = findView(R.id.swipe_recyce_view);
    }

    @Override
    protected void setDefaultValues() {
        setCenterTitleName("报警未处理");
        mAdapter = new AlarmNodoAdapter(AlarmNoDoListActivity.this);
        mList = new ArrayList<>();
        mRefreshRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        mRefreshRecyclerView.setAdapter(mAdapter);
        mRefreshRecyclerView.getScrollView().addItemDecoration(new RecycleViewDivider(
                mContext, LinearLayoutManager.VERTICAL, ScreenUtil.dp2pix(1), getResources().getColor(R.color.main_line_gray)));
        mAdapter.refreshData(mList);
        mRefreshRecyclerView.setOnListLoadListener(this);
        mRefreshRecyclerView.setOnRefreshListener(this);
        mRefreshRecyclerView.setRefreshing(true);
        mAdapter.setOnItemClickListener(new BaseRecyclerAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseRecyclerViewHolder holder) {
                int position = mRefreshRecyclerView.getScrollView().getChildLayoutPosition(holder.itemView);
                AlarmNodoResponse.AlarmDev dev = mList.get(position);
                DeviceItem deviceItem = new DeviceItem();
                deviceItem.efairydevice_id = Long.valueOf(dev.efairydevice_id);
                deviceItem.efairydevice_name = dev.efairydevice_name;
                ActivityUtils.startChattingActivity(AlarmNoDoListActivity.this, REQUEST_CODE_ITEM, deviceItem);
            }
        });
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    @Override
    public void onListLoad() {
        loadMore();
    }

    private void refresh() {
        HttpManager.getInstance().sendRequest(new GetAlarmNodoListRequest(0, "",
                new Response.Listener<AlarmNodoResponse>() {
                    @Override
                    protected void onSuccess(AlarmNodoResponse response) {
                        if (response != null && response.unhandle_device_list != null) {
                            mList.clear();
                            mList.addAll(response.unhandle_device_list);
                            mAdapter.notifyDataSetChanged();
                            if (GetAnnounceListRequest.size > mList.size()) {
                                mRefreshRecyclerView.setEnabledLoad(false);
                            } else {
                                mRefreshRecyclerView.setEnabledLoad(true);
                            }
                        }
                        mRefreshRecyclerView.setRefreshing(false);
                        showTip(mList);
                    }

                    @Override
                    protected void onFail(NetworkException exception) {
                        doException(exception);
                        mRefreshRecyclerView.setRefreshing(false);
                        showTip(mList);
                    }
                }));
    }

    private void loadMore() {
        if (mList.size() == 0) {
            mRefreshRecyclerView.setLoading(false);
            return;
        }
        HttpManager.getInstance().sendRequest(new GetAlarmNodoListRequest(mList.get(mList.size() - 1).efairydevice_id, "",
                new Response.Listener<AlarmNodoResponse>() {
                    @Override
                    protected void onSuccess(AlarmNodoResponse response) {
                        if (response != null && response.unhandle_device_list != null) {

                            mList.addAll(response.unhandle_device_list);
                            mAdapter.notifyDataSetChanged();
                            if (mList.size() == response.total_rows) {
                                mRefreshRecyclerView.setEnabledLoad(false);
                            } else {
                                mRefreshRecyclerView.setEnabledLoad(true);
                            }
                        }
                        mRefreshRecyclerView.setLoading(false);
                    }

                    @Override
                    protected void onFail(NetworkException exception) {
                        doException(exception);
                        mRefreshRecyclerView.setLoading(false);
                    }
                }));
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case REQUEST_CODE_ADD:
                case REQUEST_CODE_ITEM:
                    mRefreshRecyclerView.setRefreshing(true);
                    setResult(RESULT_OK);
                    break;
                default:
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
